package com.aloudmeta.grpc.service;

import com.aloudmeta.grpc.demo.DubboEchoServiceGrpc;
import com.aloudmeta.grpc.demo.EchoRequest;
import com.aloudmeta.grpc.demo.EchoResponse;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * huaixin 2022/1/21 2:34 PM
 */
@DubboService(version = "1.0.0")
@Slf4j
public class DubboEchoService extends DubboEchoServiceGrpc.EchoServiceImplBase
    implements DubboEchoServiceGrpc.IEchoService {


    @Override
    public void echo(EchoRequest request, StreamObserver<EchoResponse> observer) {
        String message = request.getMessage();
        String reply = "Hello " + message;
        log.info(reply);

        //throw new IllegalArgumentException("参数不正确");
        for (int i = 0; i < 10; i++) {
            EchoResponse response = EchoResponse.newBuilder()
                    .setMessage(reply + " " + i)
                    .build();
            observer.onNext(response);
        }
        observer.onCompleted();
    }
}